import os
from pathlib import Path
from datetime import datetime
import numpy
#指定路径
folder_path = "D:\\Firmware\\Python_Project\\1st_lag_calculate\\14.000"
# 获取文件列表
files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
# 获取每个文件的创建时间并按创建时间排序
files_with_ctime = [(f, datetime.fromtimestamp(os.path.getctime(f))) for f in files]
files_with_ctime.sort(key=lambda x: x[1])
# 按创建时间排序后的文件列表
sorted_files = [f[0] for f in files_with_ctime]
# # 打印按照创建时间排序的结果
# for file in sorted_files:
#     print(file)
#获取亮场图
raw_mean_max = 0
for raw_path in sorted_files :
    raw = numpy.fromfile(raw_path,dtype='uint16')
    raw_mean = numpy.mean(raw)
    if raw_mean > raw_mean_max :
        raw_mean_max = raw_mean
        raw_light_position = sorted_files.index(raw_path)
print("亮场图:",sorted_files[raw_light_position],"平均灰度值:",raw_mean_max)
#获取暗场模板图
raw_dark_temp = numpy.fromfile(sorted_files[2],dtype='uint16')
raw_dark_temp_mean = numpy.mean(raw_dark_temp)
print("暗场图:",sorted_files[2],"平均灰度值:",raw_dark_temp_mean)
#计算LAG
raw_lag_list = sorted_files[raw_light_position+1:len(sorted_files)]
for file in raw_lag_list :
    raw_lag_mean = numpy.mean(numpy.fromfile(file,dtype='uint16'))
    lag = (raw_lag_mean - raw_dark_temp_mean) / (raw_mean_max - raw_dark_temp_mean)
    lag = format(lag,'.4%')
    print(lag)
    # print(raw_lag_list.index(file)+1,lag)